home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / HotLinks.subproj / HotLinks.m < prev    next >
Encoding:
Text File  |  1995-01-25  |  4.3 KB  |  168 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //    FILENAME:    HotLinks.m
  3. //    SUMMARY:    Implementation of a link-caching Accessory
  4. //    SUPERCLASS:    Object
  5. //    INTERFACE:    HotLinks.nib
  6. //    PROTOCOLS:    <Tool>
  7. //    AUTHOR:        Rohit Khare
  8. //    COPYRIGHT:    (c) 1994 California Institure of Technology, eText Project
  9. ///////////////////////////////////////////////////////////////////////////////
  10. //    DESCRIPTION: The HotLinks panel is an example of a loadable
  11. // Accessory, an independent entity within the eText runtime
  12. // environment. The HotLinks panel allows the user to keep a
  13. // quick-reference list of links handy. The object archives itself
  14. // entire in ~/Library/eText/HotLinks (creates the dir if
  15. // nonexistent). We're being lazy, so we'll just write ourselves
  16. // out everytime we're modified -- ideally there's an <appNotification>
  17. // protocol similar to <docNotification>, and we should only save
  18. // this data out on application-quit.
  19. ///////////////////////////////////////////////////////////////////////////////
  20. //    HISTORY
  21. //    11/13/94:    Modified to use a storage for sorted hotlink lists.
  22. //    05/06/94:    Created. First actual implementation.
  23. ///////////////////////////////////////////////////////////////////////////////
  24.  
  25. #import "HotLinks.h"
  26.  
  27. @implementation HotLinks
  28.  
  29. + new 
  30. {
  31.     static HotLinks *hl = nil;
  32.     
  33.     if (!hl) {
  34.         hl = [[HotLinks alloc] init];
  35.     }
  36.     return hl;
  37. }
  38.  
  39. + toolAwake:theApp
  40. {
  41.     [theApp registerAccessory:NXUniqueString("HotLinks...")
  42.                 key:'H'
  43.                name:NXUniqueString("HotLinks")
  44.              target:[HotLinks new]
  45.              action:@selector(activate:)];
  46.     return self;
  47. }
  48.  
  49. - init
  50. {
  51.     char        buf[MAXPATHLEN];
  52.     NXBundle   *bundle;
  53.     NXStream   *s;
  54.  
  55.      bundle = [NXBundle bundleForClass:[HotLinks class]];
  56.     if ( [bundle getPath:buf forResource:"HotLinks" ofType:"nib"] ) {
  57.         [NXApp loadNibFile:buf owner:self withNames:NO];
  58.     } else {
  59.         NXLogError("NIB not found: HotLinks");
  60.     }
  61.     [well initLinkWell];
  62.     [panel setFrameAutosaveName:"etHotLinks"];
  63.     [panel setFrameUsingName:"etHotLinks"];
  64.     
  65.     sprintf(buf, "%s/.HotLinks", [userModel stringQuery:ETFDIRECTORY]);
  66.     s = NXOpenTypedStreamForFile(buf, NX_READONLY);
  67.     if (s) {
  68.         anchors = NXReadObject(s);
  69.         NXCloseTypedStream(s);
  70.     } else anchors = [[Storage alloc] initCount:0 elementSize:sizeof(etfLink) description:@encode(etfLink)];
  71.     return self;
  72. }
  73.  
  74. - free {return self;}
  75.  
  76. - save
  77. {
  78.     NXStream *s;
  79.     char     path[MAXPATHLEN];
  80.     
  81.     sprintf(path, "%s/.HotLinks", [userModel stringQuery:ETFDIRECTORY]);
  82.     // need to provide deletion and to load data in init: and to mkdir()
  83.     s = NXOpenTypedStreamForFile(path, NX_WRITEONLY);
  84.     if (s) {
  85.         NXWriteObject(s, anchors);
  86.         NXCloseTypedStream(s);
  87.     }
  88.     return self;
  89. }
  90.  
  91. - activate:sender
  92. {
  93.     [panel makeKeyAndOrderFront:self];
  94.     [panel makeFirstResponder:browser];
  95.     return self;
  96. }
  97.  
  98. - delete:sender
  99. {
  100.     int i = [[browser matrixInColumn:0] selectedRow];
  101.  
  102.     [anchors removeElementAt:i];
  103.     [self save];
  104.     [well setLink:NULL];
  105.     [field setStringValue:""];
  106.     [browser loadColumnZero];
  107.     [self select:browser];
  108.     [panel makeFirstResponder:browser];
  109.     return self;
  110.  
  111. }
  112.  
  113. - accept:sender
  114. {
  115.     [anchors insertElement:[sender currentLink] at:0];
  116.     [self save];
  117.     [well setLink:NULL];
  118.     [field setStringValue:""];
  119.     [browser loadColumnZero];
  120.     [self select:browser];
  121.     return self;
  122. }
  123.  
  124. - rename:sender
  125. {
  126.     int i = [[browser matrixInColumn:0] selectedRow];
  127.  
  128.     ((etfLink *)[anchors elementAt:i])->anchorTitle = NXUniqueString([field stringValue]);
  129.     [self save];
  130.     [browser loadColumnZero];
  131.     [browser setPath:((etfLink *)[anchors elementAt:i])->anchorTitle];
  132.     i = [[browser matrixInColumn:0] selectedRow];
  133.     [well setLink:[anchors elementAt:i]];
  134.     [panel makeFirstResponder:browser];
  135.     return self;
  136. }
  137.  
  138. - select:sender
  139. {
  140.     int i = [[browser matrixInColumn:0] selectedRow];
  141.     //reflect the selection
  142.     [field setStringValue:((etfLink *)[anchors elementAt:i])->anchorTitle];
  143.     [well setLink:[anchors elementAt:i]];
  144.     return self;
  145. }
  146.  
  147. static int eTHotLinks_docompare(etfLink *x, etfLink *y)
  148. {
  149.     return strcasecmp(x->anchorTitle, y->anchorTitle);
  150. }
  151.  
  152. - (int) browser:theBrowser fillMatrix:theMatrix inColumn:(int) col 
  153. {
  154.     int        rows,i;
  155.     id        cell;
  156.     
  157.     rows = [anchors count];
  158.     qsort(anchors->dataPtr, anchors->numElements, anchors->elementSize, eTHotLinks_docompare);
  159.     for (i=0; i <rows; i++) {
  160.         [theMatrix addRow];
  161.         cell = [theMatrix cellAt:i :0];
  162.         [cell setStringValueNoCopy:((etfLink *)[anchors elementAt:i])->anchorTitle];
  163.         [cell setLoaded:YES];
  164.         [cell setLeaf:YES];
  165.     }
  166.     return rows;
  167. }
  168. @end